# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

include $(NACL_SDK_ROOT)/tools/common.mk

TARGET = python
LIBS = python3.4m readline ncurses bz2 z tar
LIBS += ppapi_simple nacl_io ppapi
NACL_LDFLAGS += $(NACL_CLI_MAIN_LIB)
PNACL_LDFLAGS += $(NACL_CLI_MAIN_LIB)
ifeq ($(TOOLCHAIN),glibc)
LIBS += util dl
NACL_LDFLAGS += -Wl,-export-dynamic
else
LIBS += intl glibc-compat
endif
SOURCES = python.c
INC_PATHS = $(NACLPORTS_INCLUDE)/python3.4m
INSTALL_DIR = $(NACL_PACKAGES_PUBLISH)/python3/$(TOOLCHAIN)
ifeq ($(TOOLCHAIN),pnacl)
INSTALL_ARCH_DIR = $(INSTALL_DIR)
else
INSTALL_ARCH_DIR = $(INSTALL_DIR)/_platform_specific/$(NACL_ARCH)
endif

ifeq ($(TOOLCHAIN),pnacl)
EXEEXT=.pexe
else
EXEEXT=.nexe
endif

ifneq ($(TOOLCHAIN),pnacl)
# We want the nmf to contain all the .nexe we have previously
# built as well as the one currently being built, so we modify
# the list of executables that we pass to create_nmf.
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_64$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_32$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_arm$(EXEEXT))
endif

# Build rules generated by macros from common.mk:

$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))

ifeq ($(CONFIG),Release)
$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
else
$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
endif

$(eval $(call NMF_RULE,$(TARGET),))

ifeq ($(TOOLCHAIN),glibc)
EXTRA_ASSETS = lib/libz.so.1 lib/libreadline.so lib/libncurses.so.5
endif

CHROMEAPPS = $(NACL_SRC)/third_party/libapps/
LIB_DOT = $(CHROMEAPPS)/libdot
HTERM = $(CHROMEAPPS)/hterm

install-hterm:
	mkdir -p $(INSTALL_DIR)
	mkdir -p $(INSTALL_ARCH_DIR)
	LIBDOT_SEARCH_PATH=$(CHROMEAPPS) $(LIB_DOT)/bin/concat.sh -i $(HTERM)/concat/hterm_deps.concat -o $(INSTALL_DIR)/hterm.concat.js
	LIBDOT_SEARCH_PATH=$(CHROMEAPPS) $(LIB_DOT)/bin/concat.sh -i $(HTERM)/concat/hterm.concat -o $(INSTALL_DIR)/hterm2.concat.js
	chmod +w $(INSTALL_DIR)/hterm2.concat.js $(INSTALL_DIR)/hterm.concat.js
	cat $(INSTALL_DIR)/hterm2.concat.js >> $(INSTALL_DIR)/hterm.concat.js
	rm $(INSTALL_DIR)/hterm2.concat.js
	cp ../../build_tools/naclterm.js $(INSTALL_DIR)
	cp ../../build_tools/pipeserver.js $(INSTALL_DIR)
	cp ../../build_tools/naclprocess.js $(INSTALL_DIR)
ifeq ($(TOOLCHAIN),pnacl)
	sed -i.bak 's/x-nacl/x-pnacl/g' $(INSTALL_DIR)/naclprocess.js
endif

install: install-hterm
	mkdir -p $(INSTALL_DIR)
	tar -h -c --exclude=*.pyc --exclude=__pycache__ -C ${NACL_PREFIX} -f $(INSTALL_ARCH_DIR)/pydata_$(NACL_ARCH).tar lib/python3.4 ${EXTRA_ASSETS}
ifeq ($(TOOLCHAIN),pnacl)
	cp $(OUTDIR)/python$(EXEEXT) $(INSTALL_ARCH_DIR)
	cd $(INSTALL_DIR) && \
	  python $(NACL_SDK_ROOT)/tools/create_nmf.py \
	  python$(EXEEXT) -s . -o python.nmf
else
	cp $(OUTDIR)/python_$(NACL_ARCH)$(EXEEXT) $(INSTALL_ARCH_DIR)
	cd $(INSTALL_DIR) && \
	  python $(NACL_SDK_ROOT)/tools/create_nmf.py \
	  _platform_specific/*/python_*$(EXEEXT) \
	  -L $(OUTDIR)/lib32 -L $(OUTDIR)/lib64 \
	  -s . -o python.nmf
endif
	cp index.html $(INSTALL_DIR)
	cp python.js $(INSTALL_DIR)
	cp manifest.json $(INSTALL_DIR)
	cp background.js $(INSTALL_DIR)
	cp icon_16.png $(INSTALL_DIR)
	cp icon_48.png $(INSTALL_DIR)
	cp icon_128.png $(INSTALL_DIR)
	cd $(INSTALL_DIR) && rm -f python.zip && zip -r python.zip .

.PHONY: install-hterm
